<template>
  <div class="ad-cube" :style="containerStyle">
    <div class="ad-content" :class="`layout-${property.layout}`">
      <!-- 左侧广告区域 -->
      <div class="left-ads">
        <!-- 顶部广告 -->
        <div class="ad-item top-ad" @click="handleAdClick(property.leftAds.top)">
          <img 
            v-if="property.leftAds.top.imgUrl" 
            :src="property.leftAds.top.imgUrl" 
            :alt="property.leftAds.top.title"
            class="ad-image"
            @error="handleImageError"
          />
          <div class="ad-overlay">
            <div class="ad-text">
              <h3 v-if="property.leftAds.top.title" class="ad-title">
                {{ property.leftAds.top.title }}
              </h3>
              <p v-if="property.leftAds.top.subtitle" class="ad-subtitle">
                {{ property.leftAds.top.subtitle }}
              </p>
              <div v-if="property.leftAds.top.action" class="ad-action">
                {{ property.leftAds.top.action }}
              </div>
            </div>
          </div>
        </div>
        
        <!-- 底部广告 -->
        <div class="ad-item bottom-ad" @click="handleAdClick(property.leftAds.bottom)">
          <img 
            v-if="property.leftAds.bottom.imgUrl" 
            :src="property.leftAds.bottom.imgUrl" 
            :alt="property.leftAds.bottom.title"
            class="ad-image"
            @error="handleImageError"
          />
          <div class="ad-overlay">
            <div class="ad-text">
              <h3 v-if="property.leftAds.bottom.title" class="ad-title">
                {{ property.leftAds.bottom.title }}
              </h3>
              <p v-if="property.leftAds.bottom.subtitle" class="ad-subtitle">
                {{ property.leftAds.bottom.subtitle }}
              </p>
            </div>
            <img 
              v-if="property.leftAds.bottom.decorationImg" 
              :src="property.leftAds.bottom.decorationImg" 
              class="decoration-img"
              @error="handleImageError"
            />
          </div>
        </div>
      </div>
      
      <!-- 右侧广告区域 -->
      <div class="right-ads">
        <!-- 轮播广告 -->
        <div class="banner-container">
          <el-carousel 
            v-if="property.rightAds.banners.length > 0"
            :autoplay="property.bannerConfig.autoplay"
            :interval="property.bannerConfig.interval"
            :duration="property.bannerConfig.duration"
            indicator-position="none"
            arrow="never"
            height="200px"
          >
            <el-carousel-item 
              v-for="(banner, index) in property.rightAds.banners" 
              :key="index"
            >
              <div class="banner-item" @click="handleAdClick(banner)">
                <img 
                  :src="banner.imgUrl" 
                  :alt="banner.title"
                  class="banner-image"
                  @error="handleImageError"
                />
                <div class="banner-overlay">
                  <div class="banner-text">
                    <h3 v-if="banner.title" class="banner-title">{{ banner.title }}</h3>
                    <p v-if="banner.subtitle" class="banner-subtitle">{{ banner.subtitle }}</p>
                    <div v-if="banner.action" class="banner-action">{{ banner.action }}</div>
                  </div>
                </div>
              </div>
            </el-carousel-item>
          </el-carousel>
        </div>
        
        <!-- 底部项目 -->
        <div class="bottom-items">
          <div 
            v-for="(item, index) in property.rightAds.bottomItems" 
            :key="index"
            class="bottom-item"
            @click="handleAdClick(item)"
          >
            <img 
              v-if="item.imgUrl" 
              :src="item.imgUrl" 
              :alt="item.title"
              class="item-image"
              @error="handleImageError"
            />
            <div class="item-content">
              <h4 v-if="item.title" class="item-title">{{ item.title }}</h4>
              <p v-if="item.subtitle" class="item-subtitle">{{ item.subtitle }}</p>
            </div>
            <div v-if="item.badge" class="item-badge">{{ item.badge }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { AdCubeProperty, AdItem } from './config'

defineOptions({ name: 'AdCube' })

const props = defineProps<{
  property: AdCubeProperty
}>()

// 计算容器样式
const containerStyle = computed(() => {
  const style = props.property.style
  return {
    backgroundColor: style.bgType === 'color' ? style.bgColor : 'transparent',
    backgroundImage: style.bgType === 'img' ? `url(${style.bgImg})` : 'none',
    marginTop: style.marginTop + 'px',
    marginBottom: style.marginBottom + 'px',
    marginLeft: style.marginLeft + 'px',
    marginRight: style.marginRight + 'px',
    paddingTop: style.paddingTop + 'px',
    paddingBottom: style.paddingBottom + 'px',
    paddingLeft: style.paddingLeft + 'px',
    paddingRight: style.paddingRight + 'px',
    borderRadius: style.borderRadius + 'px',
    height: style.height || '400px',
    gap: style.gap || '12px'
  }
})

// 处理广告点击
const handleAdClick = (ad: AdItem) => {
  if (ad.url) {
    // 这里可以处理跳转逻辑
    console.log('广告点击:', ad.url)
  }
}

// 处理图片加载错误
const handleImageError = (event: Event) => {
  const img = event.target as HTMLImageElement
  img.style.display = 'none'
}
</script>

<style scoped lang="scss">
.ad-cube {
  width: 100%;
  overflow: hidden;
  
  .ad-content {
    display: flex;
    gap: 12px;
    height: 100%;
    
    &.layout-leftRight {
      flex-direction: row;
    }
    
    &.layout-topBottom {
      flex-direction: column;
    }
    
    .left-ads {
      flex: 1;
      display: flex;
      flex-direction: column;
      gap: 12px;
      
      .ad-item {
        position: relative;
        flex: 1;
        border-radius: 8px;
        overflow: hidden;
        cursor: pointer;
        transition: transform 0.3s ease;
        
        &:hover {
          transform: scale(1.02);
        }
        
        .ad-image {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
        
        .ad-overlay {
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background: linear-gradient(45deg, rgba(0,0,0,0.3), rgba(0,0,0,0.1));
          display: flex;
          align-items: center;
          justify-content: space-between;
          padding: 16px;
          
          .ad-text {
            color: white;
            
            .ad-title {
              font-size: 16px;
              font-weight: bold;
              margin: 0 0 4px 0;
            }
            
            .ad-subtitle {
              font-size: 12px;
              margin: 0 0 8px 0;
              opacity: 0.9;
            }
            
            .ad-action {
              background: rgba(255, 255, 255, 0.2);
              padding: 4px 12px;
              border-radius: 12px;
              font-size: 12px;
              display: inline-block;
            }
          }
          
          .decoration-img {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            object-fit: cover;
          }
        }
      }
    }
    
    .right-ads {
      flex: 1;
      display: flex;
      flex-direction: column;
      gap: 12px;
      
      .banner-container {
        flex: 1;
        border-radius: 8px;
        overflow: hidden;
        
        .banner-item {
          position: relative;
          height: 100%;
          cursor: pointer;
          
          .banner-image {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
          
          .banner-overlay {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(45deg, rgba(0,0,0,0.4), rgba(0,0,0,0.1));
            display: flex;
            align-items: center;
            padding: 16px;
            
            .banner-text {
              color: white;
              
              .banner-title {
                font-size: 14px;
                font-weight: bold;
                margin: 0 0 4px 0;
              }
              
              .banner-subtitle {
                font-size: 12px;
                margin: 0 0 8px 0;
                opacity: 0.9;
              }
              
              .banner-action {
                background: rgba(255, 255, 255, 0.2);
                padding: 4px 12px;
                border-radius: 12px;
                font-size: 12px;
                display: inline-block;
              }
            }
          }
        }
      }
      
      .bottom-items {
        display: flex;
        flex-direction: column;
        gap: 8px;
        
        .bottom-item {
          width: 100%;
          background: white;
          border-radius: 8px;
          padding: 12px;
          display: flex;
          align-items: center;
          gap: 12px;
          cursor: pointer;
          transition: transform 0.3s ease;
          position: relative;
          
          &:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
          }
          
          .item-image {
            width: 50px;
            height: 50px;
            border-radius: 8px;
            object-fit: cover;
            flex-shrink: 0;
          }
          
          .item-content {
            flex: 1;
            
            .item-title {
              font-size: 15px;
              font-weight: bold;
              margin: 0 0 4px 0;
              color: #333;
              line-height: 1.3;
            }
            
            .item-subtitle {
              font-size: 13px;
              margin: 0;
              color: #666;
              line-height: 1.3;
            }
          }
          
          .item-badge {
            position: absolute;
            top: 8px;
            right: 8px;
            background: #ff4757;
            color: white;
            font-size: 10px;
            padding: 3px 8px;
            border-radius: 10px;
            min-width: 18px;
            text-align: center;
            font-weight: bold;
          }
        }
      }
    }
  }
}
</style>
